package Test1;

public class Num1 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9};
        int index = binarySearch(arr,5);
        System.out.println(index);

    }

    public static int binarySearch(int[] arr,int key){
        int min=0;
        int max=arr.length-1;
        int mid=0;
        while (min<=max){
            mid=(min+max)/2;
            if (key>arr[mid]){
                min=mid+1;
            }
            else if (key<arr[mid]){
                max=mid-1;
            }
            else
                return mid;

        }
        return -1;
    }
}
